﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.CustomerManagement
{
    public partial class Salesoutboundorder : BasePage
    {
        private static ISalesStockBLL salesStockBLL = IocContainer.Instance.Resolve<ISalesStockBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtStartDate.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            }
        }

        [WebMethod]
        public static string GetSalesStock(string customer, string empName, string code, string startDate, string endDate, string sidx, string sord, int page, int rows)
        {
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");

            if (!string.IsNullOrEmpty(customer))//客户
            {
                strWhere.Append(" and FCustid like '" + customer + "%'");
            }

            if (!string.IsNullOrEmpty(empName))
            {
                strWhere.Append(" and FEmpName like '" + empName + "%'");
            }
            if (!string.IsNullOrEmpty(code))
            {
                strWhere.Append(" and Fnumber= '" + code + "'");
            }
            if (!string.IsNullOrEmpty(startDate) && string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and Fdate >='" + startDate + "'");
            }
            else if (string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and Fdate <='" + endDate.Trim() + " 23:59:59'");
            }
            else if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and Fdate between '" + startDate + "' and '" + endDate.Trim() + " 23:59:59'");
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(sidx) ? " " + sidx : "  Fnumber  ");
            strOrderBy.Append(!string.IsNullOrEmpty(sord) ? " " + sord : "  desc ");

            int totalRecords = 0;
            int totalPages = 0;
            DataTable dtProducts = salesStockBLL.GetSalesStockByPage(strWhere.ToString(), strOrderBy.ToString(), page, rows, out totalRecords,
                                           out totalPages);

            string strResult = SerializationHelper.DataTable2Json(dtProducts);

            return "{\"records\":\"" + totalRecords.ToString() + "\" ,\"total\":\"" + totalPages.ToString() + "\",\"page\":\"" + page.ToString() + "\" ,\"rows\":" +
                   strResult + " }";

        }


        [WebMethod]
        public static string GetDetailedData(string id,  string sidx, string sord, int page, int rows)
        {
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            string strReturnValue = "";
            if (!string.IsNullOrEmpty(id))
            {
                strWhere.Append(" and FSaleStockID=" + id);
                StringBuilder strOrderBy = new StringBuilder(" Order by ");
                strOrderBy.Append(!string.IsNullOrEmpty(sidx) ? " " + sidx : "  FOrdernumber  ");
                strOrderBy.Append(!string.IsNullOrEmpty(sord) ? " " + sord : "  desc ");

                int totalRecords = 0;
                int totalPages = 0;
                DataTable dtProducts = salesStockBLL.GetDetailedDataByPage(strWhere.ToString(), strOrderBy.ToString(), page, rows, out totalRecords,
                                               out totalPages);

                string strResult = SerializationHelper.DataTable2Json(dtProducts);

                strReturnValue= "{\"records\":\"" + totalRecords.ToString() + "\" ,\"total\":\"" + totalPages.ToString() + "\",\"page\":\"" + page.ToString() + "\" ,\"rows\":" +
                       strResult + " }";
            }

            return strReturnValue;

        }
    }
}
